/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo2;

import Modelo.DAL.RutaEscape_DAO;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import org.json.JSONArray;
import Acceso.Security;

/**
 *
 * @author Patricia
 */
public class consultarRutaEscapeAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador"};
        if (!Security.validateSession(request, a)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String edificio = request.getParameter("edificio");
        String piso = request.getParameter("piso");
        String usada = request.getParameter("usada");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");

        String where = "SELECT "
                + "nombre_sede, nombre_edificio, id_piso, fecha_revision, usada, ruta_foto"
                + ",(Select count(*) from ruta_escape) FROM ruta_escape where (1 = 1) ";
        if (edificio != null && !edificio.equals("")) {
            where += " and (nombre_edificio = '" + edificio + "')";
        }

        if (piso != null && !piso.equals("")) {
            where += " and (id_piso = " + piso+")";
        }

        if (usada != null && !usada.equals("")) {
            where += " and (usada = '" + usada + "')";
        }

        if (limit != null && !limit.equals("0")) {
            where += " limit " + limit + " offset " + offset;
        }

        System.out.println("--------------------> WHERE: " + where);
        response.setContentType("text/json");
        JSONArray rutas = RutaEscape_DAO.consultarRutasEscape(where);
        if (rutas == null) {
            return mapping.findForward(FAILURE);
        }
        System.out.println("json " + rutas.toString());
        JSONObject rt = new JSONObject();
        rt.put("consultarRutaEscape", rutas);
        PrintWriter pw = response.getWriter();
        pw.write(rt.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
